package com.bc.bcapi.service;

import com.bc.bcmodel.domain.T_User_seller_infoModel;
import com.bc.bcmodel.req.AccountMyWalletReq;
import com.bc.bcmodel.req.SellerBonusReq;
import com.bc.bcmodel.resp.AccountWalletResp;
import com.bc.commonentity.common.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.math.BigInteger;

import com.bc.bcapi.dao.T_User_seller_bonusMapper;
import com.bc.bcmodel.domain.T_User_seller_bonusModel;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @Name T_User_seller_bonusService * @Description TODO * @Author generation * @Date 2018/8/9 15:56:31 * @Version 1.0
 **/
@Service
public class T_User_seller_bonusService {
    @Resource
    T_User_seller_bonusMapper t_User_seller_bonusMapper;

    /**
     * 保存单条数据     *      * @return
     */

    public int insert(T_User_seller_bonusModel req) {
        return t_User_seller_bonusMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */

    public int insertBatch(List<T_User_seller_bonusModel> req) {
        return t_User_seller_bonusMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */

    public T_User_seller_bonusModel findByNamedParam(T_User_seller_bonusModel req) {
        return t_User_seller_bonusMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */

    public List<T_User_seller_bonusModel> getAllList(T_User_seller_bonusModel req) {
        return t_User_seller_bonusMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */

    public PageInfo<T_User_seller_bonusModel> getList(T_User_seller_bonusModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_User_seller_bonusModel> list = t_User_seller_bonusMapper.findByNamedParamList(req);
        return new PageInfo<T_User_seller_bonusModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */

    public int update(T_User_seller_bonusModel req) {
        return t_User_seller_bonusMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */

    public int delete(BigInteger id) {
        return t_User_seller_bonusMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */

    public long count(T_User_seller_bonusModel req) {
        return t_User_seller_bonusMapper.count(req);
    }

    /**
     * 设置bonus
     */
    @Transactional(rollbackFor = {
            Exception.class}, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public JsonResult setBonus(List<SellerBonusReq> req, String user_id) throws Exception {

        JsonResult js = new JsonResult();
        int length = req.size();
        Integer one_start_value = 0;
        Integer one_end_value = 0;
        BigDecimal one_bonus_value = BigDecimal.ZERO;
        Integer two_start_value = 0;
        Integer two_end_value = 0;
        BigDecimal two_bonus_value = BigDecimal.ZERO;
        Integer three_start_value = 0;
        Integer three_end_value = 999999999;
        BigDecimal three_bonus_value = BigDecimal.ZERO;
        /**验证*/
        for (int i = 0; i < length; i++) {
            SellerBonusReq model = req.get(i);
            Integer start_count = model.getStart_count();
            Integer end_count = model.getEnd_count();
            BigDecimal value = model.getBonus_value();
            if (i == 0) {
                one_end_value = end_count;
                one_bonus_value = value;
            } else if (i == 1) {
                two_start_value = start_count;
                two_end_value = end_count;
                two_bonus_value = value;
                if (one_end_value > two_start_value) {
                    throw new Exception("第二个起始值不能小于第一个最大值");
                } else if (two_start_value > two_end_value) {
                    throw new Exception("第二个起始值不能大于结束值");
                }
            } else if (i == 2) {
                three_start_value = start_count;
                three_bonus_value = value;
                if (two_end_value > three_start_value) {
                    throw new Exception("第三个值不能小于第二个结束值");
                }
            }
        }

        /**组装*/
        List<T_User_seller_bonusModel> list = new ArrayList<T_User_seller_bonusModel>();
        T_User_seller_bonusModel oneModel = new T_User_seller_bonusModel();
        oneModel.setUser_id(user_id);
        oneModel.setStart_count(one_start_value);
        oneModel.setEnd_count(one_end_value);
        oneModel.setBonus_value(one_bonus_value);
        T_User_seller_bonusModel twoModel = new T_User_seller_bonusModel();
        twoModel.setUser_id(user_id);
        twoModel.setStart_count(two_start_value);
        twoModel.setEnd_count(two_end_value);
        twoModel.setBonus_value(two_bonus_value);
        T_User_seller_bonusModel threeModel = new T_User_seller_bonusModel();
        threeModel.setUser_id(user_id);
        threeModel.setStart_count(three_start_value);
        threeModel.setEnd_count(three_end_value);
        threeModel.setBonus_value(three_bonus_value);
        list.add(oneModel);
        list.add(twoModel);
        list.add(threeModel);
        /**批量写入*/
        t_User_seller_bonusMapper.deleteByUserId(user_id);
        t_User_seller_bonusMapper.insertBatch(list);
        return js;
    }

    public JsonResult getBonus(String userid) {
        JsonResult js = new JsonResult();
        T_User_seller_bonusModel req = new T_User_seller_bonusModel();
        req.setUser_id(userid);
        List<T_User_seller_bonusModel> list =
                t_User_seller_bonusMapper.findByNamedParamList(req);
        js.setData(list);
        return js;
    }

    /**
     * 账户信息;我的钱包
     */
    public JsonResult getMyWallet(AccountMyWalletReq req) {
        JsonResult js = new JsonResult();
        AccountWalletResp result = t_User_seller_bonusMapper.getMyWallet(req);
        if (result == null) {
            js.setData(new AccountWalletResp());
            return js;
        }
        //获取该卖家的总销量
        BigDecimal sellered_amount = t_User_seller_bonusMapper.getSellerAmount(req.getUser_id());
        sellered_amount = sellered_amount == null ? new BigDecimal("0") : sellered_amount;
        //总量=余额+已销量
        //已销量
        BigDecimal amount = result.getAmount().add(sellered_amount);
        result.setAmount(amount);
        result.setSellered_amount(sellered_amount);

        BigDecimal ratio = new BigDecimal("0");
        if (sellered_amount.intValue() != 0) {
//            ratio = (amount.divide(sellered_amount)).subtract(amount);
            ratio = (sellered_amount.divide(amount)).multiply(new BigDecimal("100"));
        }
        ratio = ratio.abs();
        ratio = ratio.setScale(2);
        result.setCompletion_ratio(ratio);
        js.setData(result);
        return js;
    }
}